package com.onadake.web.domain;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.onadake.utils.DomainUtils;
import com.onadake.utils.ValidationUtils;

/**
 * Contains errors which are result as a request processing.
 * 
 * @author radek.hecl
 *
 */
public class RequestErrors {

    /**
     * Builder object.
     */
    public static class Builder {

        /**
         * Errors binded to the fields which are uniquely identified by name.
         * Key is name of the field, value is the error message.
         */
        private Map<String, String> namedFieldErrors = new HashMap<>();

        /**
         * Sets the errors for the named fields.
         * 
         * @param namedFieldErrors map of errors to the named fields
         * @return this instance
         */
        public Builder setNamedFieldErrors(Map<String, String> namedFieldErrors) {
            this.namedFieldErrors = DomainUtils.softCopyMap(namedFieldErrors);
            return this;
        }

        /**
         * Adds named field error.
         * 
         * @param field filed name
         * @param message message which is used
         * @return this instance
         */
        public Builder addNamedFieldError(String field, String message) {
            this.namedFieldErrors.put(field, message);
            return this;
        }
        
        /**
         * Add map of field errors.
         * 
         * @param namedFieldErrors map with errors
         * @return this instance
         */
        public Builder addNamedFieldErrors(Map<String, String> namedFieldErrors) {
            this.namedFieldErrors.putAll(namedFieldErrors);
            return this;
        }
        
        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public RequestErrors build() {
            return new RequestErrors(this);
        }
    }
    
    /**
     * Errors binded to the fields which are uniquely identified by name.
     * Key is name of the field, value is the error message.
     */
    private Map<String, String> namedFieldErrors;

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public RequestErrors(Builder builder) {
        namedFieldErrors = Collections.unmodifiableMap(DomainUtils.softCopyMap(builder.namedFieldErrors));
        guardInvariants();
    }
    
    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotEmptyStringInMap(namedFieldErrors, "namedFieldErrors cannot have error with empty key or value");
    }
    
    /**
     * Returns map with all errors. 
     * 
     * @return map with all errors map of all the errors
     */
    public Map<String, Map<String, String>> getErrors() {
        return Collections.singletonMap("namedFields", namedFieldErrors);
    }
    
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
